package com.sky.controller.user;


import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.User;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.UserService;
import com.sky.utils.JwtUtil;
import com.sky.vo.UserLoginVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user/user")
public class UserController {
    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Autowired
   private UserService userService;
   @Autowired
   private JwtProperties jwtProperties;
   @PostMapping("/login")
   public Result<UserLoginVO> UserLogin(@RequestBody UserLoginDTO userLoginDTO){
           User user =  userService.UserLogin(userLoginDTO);
           UserLoginVO userLoginVO = new UserLoginVO();
       BeanUtils.copyProperties(user,userLoginVO);
       Map<String,Object> map = new HashMap<>();
       map.put(JwtClaimsConstant.USER_ID,userLoginVO.getId());
       String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(),jwtProperties.getUserTtl(),map);
       log.info("用户{}登录成功", userLoginVO.getId());
       userLoginVO.setToken(token);
      return Result.success(userLoginVO);


   }



}
